package svc

import (
	"context"
	"gozerogateway/common/errorx"
	"gozerogateway/rpc/svc/svcclient"

	"encoding/json"
	"gozerogateway/api/internal/svc"
	"gozerogateway/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ServiceListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewServiceListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ServiceListLogic {
	return &ServiceListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ServiceListLogic) ServiceList(req *types.ServiceListReq, num ...string) (resp *svcclient.ServiceListResp, err error) {
	// todo: add your logic here and delete this line

	res, err := l.svcCtx.Svc.ServiceList(l.ctx, &svcclient.ServiceListReq{
		Info:     req.Info,
		PageNo:   req.PageNo,
		PageSize: req.PageSize,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("列出服务信息失败，参数:%s，错误:%s", reqStr, err.Error())
		return nil, errorx.NewDefaultError("列出服务信息失败")
	}

	return &svcclient.ServiceListResp{
		Total: res.Total,
		List:  res.List, //////////////////////////?
	}, nil
}
